// pages/compony-pages/edit-compony/edit-compony.js
const app = getApp();
let _preEnterpriseLogo = '';
import Toast from '../../../component/toast/toast';
const uploadFile = require('../../../lib/aliyun/uploadAliyun.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        componyName:'',
        componyTrade:'',
        financeArr:[],
        scaleArr:[],
        scale:'',
        finance:'',
        enterpriseLogo:'',
        teamDes:''
    },
    /**
     * -------------------网络请求部分-------------------
     */
    getEnterpriseParams(){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.getEnterpriseParams,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function(res) {
                console.log("getEnterpriseParams success:" , res);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    that.setData({
                        financeArr: res.data.BodyContent.FinancingStage,
                        scaleArr: res.data.BodyContent.Scale
                    })
                }
            },
            fail: function (res) { console.log("getEnterpriseParams fail" , res);},
            complete: function(res) {wx.hideLoading();},
        })
    },
    uploadSyncImg(path, dir) {
        let promis = new Promise(function (resolve, reject) {
            uploadFile({
                filePath: path,
                dir: dir,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
        return promis;
    },
    editEnterpriseInfo(data){
        wx.showLoading({
            title: '保存中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.editEnterpriseInfo,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log("editEnterpriseInfo success" , res);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    app.globalData.enterpriseInfo = res.data.BodyContent;
                    wx.navigateBack({
                        delta: 1
                    });
                }
            },
            fail: function (res) { console.log("editEnterpriseInfo fail" , res)},
            complete: function(res) {wx.hideLoading();},
        })
    },
    joinData(logo){
        let data = {
            Id: app.globalData.enterpriseInfo.Id,
            EnnterpriseLogo: logo,
            ShortName: "",
            Scale: this.data.scale,
            FinancingStage: this.data.finance,
            TeamDes: this.data.teamDes,
            Province: '',
            City: '',
            District: '',
            Address: ''
        };
        console.log("send data" , data);
        return data;
    },
    /**
     * -------------------网络请求部分-------------------
     */

    /**
     * ===================事件处理部分===================
     */
    scalePickerChange(e) {
        let data = {
            scale: this.data.scaleArr[e.detail.value]
        }
        this.setData(data);
    },
    financingPickerChange(e){
        let data = {
            finance: this.data.financeArr[e.detail.value]
        }
        this.setData(data);
    },
    chooseLogo(){
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success(res) {
                that.setData({
                    enterpriseLogo: res.tempFilePaths[0]
                });
            }
        })
    },
    inputIntroduce(e){
        this.setData({
            teamDes: e.detail.value
        });
    },
    clickSave(){
        if (!this.data.enterpriseLogo){
            Toast("请上传公司Logo");
            return;
        }
        if (!this.data.teamDes){
            Toast("请填写团队信息");
            return;
        }
        let that = this;
        if (this.data.enterpriseLogo != _preEnterpriseLogo){
            //上传图片至OSS
            wx.showLoading({
                title: '图片保存中...',
                mask: true
            });
            let dir = 'xianjian_wx_image';
            let imgPath = this.data.enterpriseLogo;
            // console.log(imgPath);
            that.uploadSyncImg(imgPath, dir).then(res => {
                if (res.statusCode == 200) {
                    let imgUrl = res.imgUrl;
                    let data = that.joinData(imgUrl);
                    that.editEnterpriseInfo(data);
                }
                console.log("upload success：", res);
                wx.hideLoading();
            }, err => {
                wx.hideLoading();
                console.log("err:", err);
                Toast("图片上传失败！")
            });
        }else{
            let data = that.joinData(this.data.enterpriseLogo);
            that.editEnterpriseInfo(data);
        }
    },
    /**
     * ===================事件处理部分===================
     */
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            componyName: app.globalData.enterpriseInfo.EnterpriseName,
            componyTrade: app.globalData.enterpriseInfo.IndustryName,
            finance: app.globalData.enterpriseInfo.FinancingStage,
            scale: app.globalData.enterpriseInfo.Scale,
            enterpriseLogo: app.globalData.enterpriseInfo.EnnterpriseLogo,
            teamDes: app.globalData.enterpriseInfo.TeamDes
        });
        _preEnterpriseLogo = app.globalData.enterpriseInfo.EnnterpriseLogo
        this.getEnterpriseParams();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})